package com.example.boot.quartz.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.enums.KeyEnum;
import com.example.mapper.AdminMapper;
import com.example.mapper.ProjectMapper;
import com.example.mapper.TeamMapper;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Project;
import com.example.pojo.entity.Team;
import com.example.pojo.utils.SendMessage;
import com.example.utils.SendMessageUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目离结束后，向施工队队长发送邮件
 *
 * @author ding
 * @since 2024/7/15
 */
public class ProjectEmail extends QuartzJobBean {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SendMessageUtils sendMessageUtils;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        String projectString = (String) redisTemplate.opsForValue().get(KeyEnum.PROJECT_UNDE_UNCOMP);
        List<Project> projectList = new ArrayList<>();
        if (!StringUtils.hasLength(projectString)) {
            //获取审批通过 处于进行中未被删除的项目
            projectList = projectMapper.selectList(new LambdaQueryWrapper<Project>()
                            .eq(Project::getStatus, 3)
                            .eq(Project::getIsDeleted, 0));

            redisTemplate.opsForValue().set(KeyEnum.PROJECT_UNDE_UNCOMP, JSON.toJSONString(projectString));
        }else {
            projectList = JSON.parseArray(projectString, Project.class);
        }

        for (Project project : projectList) {
            LocalDate end = project.getEnd();
            //当前时间增加一个月与项目结束时间相同
            if (LocalDate.now().plusMonths(1L).isEqual(end)){
                Team team = teamMapper.selectById(project.getTeamId());
                //获取队长信息
                Admin captain = adminMapper.selectById(team.getCaptainId());

                String content = "尊敬的" + team .getName() + "施工队队长" + captain.getName() + "您好，" + "您的施工队负责的项目" + project.getName()
                        + "还有一个月就到截止日期了，请确保项目已完成且各个阶段没有问题，谢谢!!!";

                SendMessage sendMessage = SendMessage.builder()
                        .to(captain.getEmail())
                        .content(content)
                        .subject("项目截止日期提醒")
                        .build();

                sendMessageUtils.sendMessage(sendMessage);
            } else if (LocalDate.now().isAfter(end)) {
                Team team = teamMapper.selectById(project.getTeamId());
                //获取队长信息
                Admin captain = adminMapper.selectById(team.getCaptainId());

                String content = "尊敬的" + team .getName() + "施工队队长" + captain.getName() + "您好，" + "您的施工队负责的项目" + project.getName()
                        + "已经逾期， 请尽快完成项目";

                SendMessage sendMessage = SendMessage.builder()
                        .to(captain.getEmail())
                        .content(content)
                        .subject("项目逾期提醒")
                        .build();
                sendMessageUtils.sendMessage(sendMessage);
            }
        }
    }
}
